﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;

namespace MMOG
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            try
            {

                Client.client = new TcpClient(ipField.Text, int.Parse(port.Text));
                Client.writer = new StreamWriter(Client.client.GetStream());
                Client.reader = new StreamReader(Client.client.GetStream());

                Client.Username = userField.Text;

                Information info = new Information();
                info.add("username", userField.Text);
                info.add("password", passwordField.Text);

                Client.writer.WriteLine(info);
                Client.writer.Flush();
                String reply = Client.reader.ReadLine();
                if (reply.Equals("Ok")) Client.connected = true;
                else
                {
                    Client.writer.Close();
                    Client.reader.Close();
                    MessageBox.Show("Invalid user name of password!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Cannot connect to remote server");
            }
        }

        private void quitButton_Click(object sender, EventArgs e)
        {
            Client.exited = true;
        }

        private void passwordField_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) connectButton_Click(null, null);
        }

        private void userField_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13) connectButton_Click(null, null);
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            serverList.Items.Add("Localhost");
            serverList.Items.Add("NextG Server");
            serverList.Items.Add("Caro Online");
            serverList.Items.Add("Quangviet");
            serverList.Items.Add("Custom");

            serverList.SelectedIndex = 0;

        }

        private void serverList_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (serverList.SelectedIndex)
            {
                case 0:
                    ipField.Text = "127.0.0.1";
                    port.Text = "29889";
                    ipField.ReadOnly = true;
                    port.ReadOnly = true;
                    break;
                case 1:
                    ipField.Text = "210.245.23.11";
                    port.Text = "29889";
                    ipField.ReadOnly = true;
                    port.ReadOnly = true;
                    break;
                case 2:
                    ipField.Text = "caroonline.no-ip.biz";
                    port.Text = "29889";
                    ipField.ReadOnly = true;
                    port.ReadOnly = true;
                    break;
                case 3:
                    ipField.Text = "quangviet.no-ip.biz";
                    port.Text = "29889";
                    ipField.ReadOnly = true;
                    port.ReadOnly = true;
                    break;
                case 4:
                    ipField.Text = "127.0.0.1";
                    port.Text = "29889";
                    ipField.ReadOnly = false;
                    port.ReadOnly = false;
                    break;
            }
        }
    }
}
